package exteditapp;

import java.util.ArrayList;

/**
 * Interface to settings parser
 * @author Arturas Smorgun, arturas@smorgun.lt
 */
public abstract class ISettings  {

	/**
	 * Loaded parameters.
	 */
	protected ArrayList<SettingsEntry> parameters = new ArrayList<SettingsEntry>();

	/**
	 * Default constructor
	 */
	public ISettings(){
	}
	
	/**
	 * Abstract method for loading of main settings.
	 * @return sign if loading succeeded or not.
	 */
	public abstract Boolean load();
	
	/**
	 * Return parameter
	 * @return parameter value, if found, or null if not
	 */	
	public String getParameter(String name){
		SettingsEntry result = getSettingsEntry(name);
		if(result != null)
			return result.getValue();
		return "";
	}

	/**
	 * Overrides some default settings (will be valid only for this runtime).
	 * @param name parameter name, which we will add or override
	 * @param value new parameter value
	 * @return true if added and false otherwise
	 */
	public Boolean setRuntimeParameter(String name, String value) {
		SettingsEntry dublicate = getSettingsEntry(name);
		if(dublicate != null){
			parameters.remove(dublicate);
		}
		parameters.add(new SettingsEntry(name, value));
		return true;
	}

	/**
	 * Returns SettingsEntry with specified name.
	 * @param name search criteria
	 */
	protected SettingsEntry getSettingsEntry(String name){
		for(int i = 0; i < parameters.size(); i++){
			if(parameters.get(i).getName().compareTo(name) == 0){
				return parameters.get(i);
			}
		}
		return null;
	}
}
